added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSEFCodeOnly / EntityConfigurations.cs
blob99b713d9b6f13edefd086ea2b20fdd7ce7618b27
1 /****************************** Module Header ******************************\
2 * Module Name: EntityConfigurations.cs
3 * Project: CSEFCodeOnly
4 * Copyright (c) Microsoft Corporation.
6 * This code file contains the EntityConfiguration for the POCO entities to
7 * create the Entity Data Model metadata.
9 * This source is subject to the Microsoft Public License.
10 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 * All other rights reserved.
13 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
14 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
16 \***************************************************************************/
18 #region Using directive
19 using System;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System.Text;
23 using Microsoft.Data.Objects;
24 #endregion
26 namespace CSEFCodeOnly
28 #region EntityConfiguration for TPT inheritance POCO entities
29 public class PersonTPTConfiguration : EntityConfiguration<PersonTPT>
31 public PersonTPTConfiguration()
33 // Set the entity key
34 HasKey(p => p.PersonID);
36 // Set the table mapping of the TPT inheritance entities
37 // The table name here is "PeopleTPT"
38 // Anonymous type properties are mapped to the table column names
39 MapHierarchy(
40 p => new
42 p.PersonID,
43 Name_FirstName = p.Name.FirstName,
44 Name_LastName = p.Name.LastName,
45 Address_City = p.Address.City,
46 Address_Country = p.Address.Country,
47 Address_Zipcode = p.Address.Zipcode
48 }).ToTable("PeopleTPT");
52 public class InstructorTPTConfiguration :
53 EntityConfiguration<InstructorTPT>
55 public InstructorTPTConfiguration()
57 // Set the regular property column mapping
58 Property(i => i.HireDate);
60 // Set the many-to-many relationship between the Course and
61 // InstructorTPT entities
62 Relationship(i => i.Courses).FromProperty(c => c.Instructors);
64 // Set the table mapping of the TPT inheritance entities
65 // The table name here is "InstructorsTPT"
66 // Anonymous type properties are mapped to the table column names
67 MapHierarchy(
68 i => new
70 i.PersonID,
71 i.HireDate
72 }).ToTable("InstructorsTPT");
76 public class StudentTPTConfiguration : EntityConfiguration<StudentTPT>
78 public StudentTPTConfiguration()
80 // Set the regular property column mappings
81 Property(s => s.EnrollmentDate);
82 Property(s => s.Degree);
83 Property(s => s.Credits);
85 // Set the table mapping of the TPT inheritance entities
86 // The table name here is "StudentsTPT"
87 // Anonymous type properties are mapped to the table column names
88 MapHierarchy(
89 s => new
91 s.PersonID,
92 s.EnrollmentDate,
93 s.Degree,
94 s.Credits
95 }).ToTable("StudentsTPT");
97 // Set the one-to-many relationship between the StudentTPT and
98 // CourseStudent entities
99 Relationship(s => s.CourseStudents)
100 .FromProperty(cs => cs.Student);
104 public class AdminTPTConfiguration : EntityConfiguration<AdminTPT>
106 public AdminTPTConfiguration()
108 // Set the regular property column mapping
109 Property(a => a.AdminDate);
111 // Set the table mapping of the TPT inheritance entities
112 // The table name here is "AdminsTPT"
113 // Use the EntityMap.Row and EntityMap.Column to map the
114 // properties to the table columns
115 MapHierarchy(
116 a => EntityMap.Row(
117 EntityMap.Column(a.PersonID, "PersonID"),
118 EntityMap.Column(a.AdminDate, "AdminDate")
119 )).ToTable("AdminsTPT");
123 public class BusinessStudentTPTConfiguration :
124 EntityConfiguration<BusinessStudentTPT>
126 public BusinessStudentTPTConfiguration()
128 // Set the regular property column mapping
129 Property(bs => bs.BusinessCredits);
131 // Set the table mapping of the TPT inheritance entities
132 // The table name here is "BusinessStudentsTPT"
133 // Use the EntityMap.Row and EntityMap.Column to map the
134 // properties to the table columns
135 MapHierarchy(
136 bs => EntityMap.Row(
137 EntityMap.Column(bs.PersonID, "PersonID"),
138 EntityMap.Column(bs.BusinessCredits, "BusinessCredits")
139 )).ToTable("BusinessStudentsTPT");
142 #endregion
144 #region EntityConfiguration for other relational POCO entities
145 public class DepartmentConfiguration : EntityConfiguration<Department>
147 public DepartmentConfiguration()
149 // Set the entity key
150 HasKey(d => d.DepartmentID);
152 // Set the varchar typed column, max length: 100, non-nullable
153 Property(d => d.Name).HasMaxLength(100).IsRequired();
155 // Set the decimal typed column, precision: 18, scale: 0,
156 // nullable
157 Property(d => d.Budget).Precision = 18;
158 Property(d => d.Budget).Scale = 0;
160 // Set the regular property column mapping
161 Property(d => d.StartDate);
163 // Set the one-to-many relationship between the Department and
164 // Course entities
165 Relationship(d => d.Courses).FromProperty(c => c.Department);
169 public class CourseConfiguration : EntityConfiguration<Course>
171 public CourseConfiguration()
173 // Set the entity key
174 HasKey(c => c.CourseID);
176 // Set the varchar typed column, max length: 100, non-nullable
177 Property(c => c.Title).HasMaxLength(100).IsRequired();
179 // Set the regular property column mapping
180 Property(c => c.Credits);
182 // Set the FK association between the Department and Course
183 // entities, and set the FK association property as
184 // Course.DepartmentID
185 // IsRequired() indicates it is 1:* relationship instead of
186 // 0..1:* relationship
187 Relationship(c => c.Department).IsRequired()
188 .FromProperty(d => d.Courses).HasConstraint(
189 (c, d) => c.DepartmentID == d.DepartmentID);
191 // Set the one-to-many relationship between the Course and
192 // CourseStudent entities
193 Relationship(c => c.CourseStudents)
194 .FromProperty(cs => cs.Course);
196 // Set the many-to-many relationship between the Course and
197 // InstructorTPT entities
198 Relationship(c => c.Instructors)
199 .FromProperty(i => i.Courses);
203 public class CourseStudentConfiguration :
204 EntityConfiguration<CourseStudent>
206 public CourseStudentConfiguration()
208 // Set the composite entity key
209 HasKey(cs => new { cs.PersonID, cs.CourseID });
211 // Set the regular property column mappings
212 Property(cs => cs.PersonID);
213 Property(cs => cs.CourseID);
214 Property(cs => cs.Score);
216 // Set the FK association between the Course and CourseStudent
217 // entities, and set the FK association property as
218 // CourseStudent.CourseID
219 Relationship(cs => cs.Course).IsRequired()
220 .FromProperty(c => c.CourseStudents)
221 .HasConstraint((cs, c) => cs.CourseID == c.CourseID);
223 // Set the FK association between the StudentTPT and
224 // CourseStudent entities, and set the FK association property
225 // as CourseStudent.PersonID
226 Relationship(cs => cs.Student).IsRequired()
227 .FromProperty(s => s.CourseStudents)
228 .HasConstraint((cs, s) => cs.PersonID == s.PersonID);
231 #endregion
233 #region EntityConfiguration for TPH inheritance POCO entities
234 public class PersonTPHConfiguration : EntityConfiguration<PersonTPH>
236 public PersonTPHConfiguration()
238 // Set the entity key
239 HasKey(p => p.PersonID);
241 // Set the table mapping of the TPT inheritance entities
242 // The table name here is "PeopleTPH"
243 // Each type has its own property and column mappings
244 // The base and each derived type have the discriminitor column
245 // "PersonCategory"
246 MapHierarchy()
247 .Case<PersonTPH>(p => new
249 p.PersonID,
250 p.Name.FirstName,
251 p.Name.LastName,
252 p.Address.City,
253 p.Address.Country,
254 p.Address.Zipcode,
255 PersonCategory = 0
257 .Case<InstructorTPH>(i => new
259 i.PersonID,
260 i.Name.FirstName,
261 i.Name.LastName,
262 i.Address.City,
263 i.Address.Country,
264 i.Address.Zipcode,
265 i.HireDate,
266 PersonCategory = 1
268 .Case<StudentTPH>(s => new
270 s.PersonID,
271 s.Name.FirstName,
272 s.Name.LastName,
273 s.Address.City,
274 s.Address.Country,
275 s.Address.Zipcode,
276 s.EnrollmentDate,
277 PersonCategory = 2
279 .Case<AdminTPH>(a => new
281 a.PersonID,
282 a.Name.FirstName,
283 a.Name.LastName,
284 a.Address.City,
285 a.Address.Country,
286 a.Address.Zipcode,
287 a.AdminDate,
288 PersonCategory = 3
289 }).ToTable("PeopleTPH");
292 #endregion
294 #region ComplexTypeCinfiguration for Complex Type entities
295 public class ComplexTypeNameConfiguration :
296 ComplexTypeConfiguration<Name>
298 public ComplexTypeNameConfiguration()
300 // Set the two varchar typed column the max length and
301 // non-nullable
302 Property(n => n.FirstName).IsMax().IsRequired();
303 Property(n => n.LastName).IsMax().IsRequired();
307 public class ComplexTypeAddressConfiguration :
308 ComplexTypeConfiguration<Address>
310 public ComplexTypeAddressConfiguration()
312 // Set the three varchar typed column the max length
313 Property(a => a.City).IsMax();
314 Property(a => a.Country).IsMax();
315 Property(a => a.Zipcode).IsMax();
318 #endregion